/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package com.alien.admin.modules.message.service.impl;

import com.alien.admin.modules.message.domain.NotifyAnnounceUser;
import com.alien.admin.utils.ValidationUtil;
import com.alien.admin.utils.FileUtil;
import lombok.RequiredArgsConstructor;
import com.alien.admin.modules.message.repository.NotifyAnnounceUserRepository;
import com.alien.admin.modules.message.service.NotifyAnnounceUserService;
import com.alien.admin.modules.message.service.dto.NotifyAnnounceUserDto;
import com.alien.admin.modules.message.service.dto.NotifyAnnounceUserQueryCriteria;
import com.alien.admin.modules.message.service.mapstruct.NotifyAnnounceUserMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import com.alien.admin.utils.PageUtil;
import com.alien.admin.utils.QueryHelp;

import java.sql.Timestamp;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;

/**
* @website https://el-admin.vip
* @description 服务实现
* @author zhouseyi
* @date 2021-11-17
**/
@Service
@RequiredArgsConstructor
public class NotifyAnnounceUserServiceImpl implements NotifyAnnounceUserService {

    private final NotifyAnnounceUserRepository notifyAnnounceUserRepository;
    private final NotifyAnnounceUserMapper notifyAnnounceUserMapper;

    @Override
    public Map<String,Object> queryAll(NotifyAnnounceUserQueryCriteria criteria, Pageable pageable){
        Page<NotifyAnnounceUser> page = notifyAnnounceUserRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(notifyAnnounceUserMapper::toDto));
    }

    @Override
    public List<NotifyAnnounceUserDto> queryAll(NotifyAnnounceUserQueryCriteria criteria){
        return notifyAnnounceUserMapper.toDto(notifyAnnounceUserRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    @Transactional
    public NotifyAnnounceUserDto findById(Integer id) {
        NotifyAnnounceUser notifyAnnounceUser = notifyAnnounceUserRepository.findById(id).orElseGet(NotifyAnnounceUser::new);
        ValidationUtil.isNull(notifyAnnounceUser.getId(),"NotifyAnnounceUser","id",id);
        return notifyAnnounceUserMapper.toDto(notifyAnnounceUser);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public NotifyAnnounceUserDto create(NotifyAnnounceUser resources) {
        return notifyAnnounceUserMapper.toDto(notifyAnnounceUserRepository.save(resources));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(NotifyAnnounceUser resources) {
        Timestamp timestamp = new Timestamp(System.currentTimeMillis());
        resources.setReadTime(timestamp);

        NotifyAnnounceUser notifyAnnounceUser = notifyAnnounceUserRepository.findById(resources.getId()).orElseGet(NotifyAnnounceUser::new);
        ValidationUtil.isNull( notifyAnnounceUser.getId(),"NotifyAnnounceUser","id",resources.getId());
        notifyAnnounceUser.copy(resources);
        notifyAnnounceUserRepository.save(notifyAnnounceUser);
    }

    @Override
    public void deleteAll(Integer[] ids) {
        for (Integer id : ids) {
            notifyAnnounceUserRepository.deleteById(id);
        }
    }

    @Override
    public void download(List<NotifyAnnounceUserDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (NotifyAnnounceUserDto notifyAnnounceUser : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("消息id", notifyAnnounceUser.getAnnounceId());
            map.put("接收用户id", notifyAnnounceUser.getRecipientId());
            map.put("创建时间", notifyAnnounceUser.getCreateTime());
            map.put("阅读时间", notifyAnnounceUser.getReadTime());
            map.put("已读状态", notifyAnnounceUser.getState());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}
